<template>
  <view class="login-container">
    <uni-icons type="contact-filled" size="100" color="#afafaf"></uni-icons>
    <button type="primary" class="btn-login" open-type="getUserInfo" @getuserinfo="getuserinfo">一键登录</button>
    <view class="tips-text">
      登陆后尽享更多权益
    </view>
  </view>
</template>

<script>
  import {mapMutations,mapState} from 'vuex'
  export default {
    name:"my-login",
    data() {
      return {
        
      };
    },
    computed:{
      ...mapState('m_user',['redirectInfo'])
    },
    methods:{
      getuserinfo(e){
        this.updateUserinfo(e.detail.userInfo)
        
        // 获取登录成功后的 Token 字符串
        this.getToken(e.detail)
        
      },
      async getToken(info){
        const [err,res]=await uni.login().catch(err=>err)
        if(err || res.errMsg !== 'login:ok'){
          uni.$showMsg('登录失败！')
        }
        console.log(info,res.code);
        const query={
          code:res.code,
          encryptedData:info.encryptedData,
          iv:info.iv,
          rawData:info.rawData,
          signature:info.signature
        }
        const {data:loginResult}=await uni.$http.post('/api/public/v1/users/wxlogin',query)
        this.updateToken('Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjEyLCJpYXQiOjE1MjU0MDIyMjMsImV4cCI6MTUyNTQ4ODYyM30.g-4GtEQNPwT_Xs0Pq7Lrco_9DfHQQsBiOKZerkO-O-o')
        // if(loginResult.meta.status!==200) return uni.$showMsg('登陆失败')
        // uni.$showMsg('登陆成功')
        this.navigateBack()
      },
      ...mapMutations('m_user',['updateUserinfo','updateToken','updateRedirectInfo']),
      navigateBack(){
        if( this.redirectInfo && this.redirectInfo.openType==='switchTab' ){
          uni.switchTab({
            url:this.redirectInfo.from,
            complete: () => {
              this.updateRedirectInfo(null)
            }
          })
        }
      }
    },
    
  }
</script>

<style lang="scss">
  .login-container{
    position: relative;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 750rpx;
    background-color: #f8f8f8;
    &::after{
      content: '';
      display: block;
      position: absolute;
      left: 0;
      bottom: 0;
      width: 100%;
      height: 40px;
      background-color: white;
      border-radius: 100%;
      transform: translateY(50%);
    }
    
    .btn-login{
      width: 90%;
      margin: 15px 0;
      border-radius: 100px;
      background-color: #c00000;
    }
    .tips-text{
      font-size: 12px;
      color: gray;
    }
  }
  
</style>